﻿using System;
using System.Collections.Generic;
using System.Text;

using System.Configuration;
using System.Data;
using System.Data.SqlClient;

namespace Aliyeye.Car.Net.Utility
{
    public static class SqlHelper
    {
        //public static readonly string connectionString = ConfigurationManager.ConnectionStrings["connectionString"].ToString();
        //public static readonly string connectionString = ConfigurationManager.ConnectionStrings["connectionStringTest"].ToString();
        //public static readonly string connectionString = ConfigurationManager.ConnectionStrings["connectionString0"].ToString();
        //public static readonly string connectionString = ConfigurationManager.ConnectionStrings["connectionString1"].ToString();
        //public static readonly string connectionString = ConfigurationManager.ConnectionStrings["connectionString2"].ToString();
        //public static readonly string connectionString = ConfigurationManager.ConnectionStrings["connectionString3"].ToString();
        //public static readonly string connectionString = ConfigurationManager.ConnectionStrings["connectionString4"].ToString();
        //public static readonly string connectionString = ConfigurationManager.ConnectionStrings["connectionString5"].ToString();
        //public static readonly string connectionString = ConfigurationManager.ConnectionStrings["connectionString6"].ToString();
        //public static readonly string connectionString = ConfigurationManager.ConnectionStrings["connectionString7"].ToString();
        //public static readonly string connectionString = ConfigurationManager.ConnectionStrings["connectionString8"].ToString();
        //public static readonly string connectionString = ConfigurationManager.ConnectionStrings["connectionStringZhu"].ToString();
        //public static readonly string connectionString = ConfigurationManager.ConnectionStrings["connectionStringYuGongSi"].ToString();
        public static readonly string connectionString = ConfigurationManager.ConnectionStrings["connectionStringYu"].ToString();

        public static DataSet GetDataFormDataSet(CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
        {
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                SqlCommand command = new SqlCommand(cmdText, conn);
                command.CommandType = cmdType;
                if (commandParameters != null)
                {
                    foreach (SqlParameter parm in commandParameters)
                        command.Parameters.Add(parm);
                }
                SqlDataAdapter adapter = new SqlDataAdapter(command);
                DataSet ds = new DataSet();
                adapter.Fill(ds);
                return ds;
            }
        }

        public static DataSet Query(string SQLString)
        {
            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                DataSet ds = new DataSet();
                try
                {
                    connection.Open();
                    SqlDataAdapter command = new SqlDataAdapter(SQLString, connection);
                    command.Fill(ds, "ds");
                }
                catch (System.Data.SqlClient.SqlException ex)
                {
                    throw new Exception(ex.Message);
                }
                finally
                {
                    connection.Close();
                }
                return ds;
            }
        }

        public static int ExecuteNonQuery(CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
        {

            SqlCommand cmd = new SqlCommand();

            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                PrepareCommand(cmd, conn, null, cmdType, cmdText, commandParameters);
                int val = cmd.ExecuteNonQuery();
                cmd.Parameters.Clear();
                return val;
            }
        }

        public static int ExecuteNonQuery(SqlTransaction trans, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
        {
            SqlCommand cmd = new SqlCommand();
            PrepareCommand(cmd, trans.Connection, trans, cmdType, cmdText, commandParameters);
            int val = cmd.ExecuteNonQuery();
            cmd.Parameters.Clear();
            return val;
        }

        public static SqlDataReader ExecuteReader(CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
        {
            SqlCommand cmd = new SqlCommand();
            SqlConnection conn = new SqlConnection(connectionString);

            try
            {
                PrepareCommand(cmd, conn, null, cmdType, cmdText, commandParameters);
                SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);

                cmd.Parameters.Clear();
                return rdr;
            }
            catch
            {
                conn.Close();
                throw;
            }
        }

        public static SqlDataReader ExecuteReader(SqlConnection conn, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
        {
            SqlCommand cmd = new SqlCommand();

            try
            {
                PrepareCommand(cmd, conn, null, cmdType, cmdText, commandParameters);
                SqlDataReader rdr = cmd.ExecuteReader();

                cmd.Parameters.Clear();
                return rdr;
            }
            catch
            {
                conn.Close();
                throw;
            }
        }

        public static object ExecuteScalar(CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
        {
            SqlCommand cmd = new SqlCommand();

            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                PrepareCommand(cmd, connection, null, cmdType, cmdText, commandParameters);
                object val = cmd.ExecuteScalar();
                cmd.Parameters.Clear();
                return val;
            }
        }

        public static object ExecuteScalar(SqlTransaction trans, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
        {
            SqlCommand cmd = new SqlCommand();

            PrepareCommand(cmd, trans.Connection, trans, cmdType, cmdText, commandParameters);
            object val = cmd.ExecuteScalar();
            cmd.Parameters.Clear();
            return val;
        }

        private static void PrepareCommand(SqlCommand cmd, SqlConnection conn, SqlTransaction trans, CommandType cmdType, string cmdText, SqlParameter[] cmdParms)
        {

            if (conn.State != ConnectionState.Open)
                conn.Open();

            cmd.Connection = conn;
            cmd.CommandText = cmdText;

            if (trans != null)
                cmd.Transaction = trans;

            cmd.CommandType = cmdType;

            if (cmdParms != null)
            {
                foreach (SqlParameter parm in cmdParms)
                    cmd.Parameters.Add(parm);
            }
        }
    }
}
